package it.gestoreContratti.gui;

import it.gestoreContratti.database.DatabaseController;
import it.gestoreContratti.database.exception.DatabaseConnectionException;
import it.gestoreContratti.database.exception.DatabaseNotFoundException;
import it.gestoreContratti.database.exception.DriverNotFoundException;
import it.gestoreContratti.database.exception.QueryException;
import it.gestoreContratti.entity.Specifica;
import it.gestoreContratti.gui.frames.NuovoContrattoFrame;
import it.gestoreContratti.gui.frames.VisualizzaSpecificheFrame;
import it.gestoreContratti.gui.panels.ContrattiScadenzaPanel;

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

public class GestoreContratti {

	private JFrame frmGestoreContratti;
	private DatabaseController databaseController;
	private ContrattiScadenzaPanel contrattiScadenzaPanel;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GestoreContratti window = new GestoreContratti();										
					window.frmGestoreContratti.setVisible(true);					
				}catch (DatabaseNotFoundException e) {
					JOptionPane.showMessageDialog(null, e.getMessage(), "", JOptionPane.ERROR_MESSAGE);					
				}catch (DriverNotFoundException e) {
					JOptionPane.showMessageDialog(null, e.getMessage(), "", JOptionPane.ERROR_MESSAGE);
				}		
				catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 * @throws DriverNotFoundException 
	 * @throws DatabaseNotFoundException 
	 */
	public GestoreContratti() throws DatabaseNotFoundException, DriverNotFoundException {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 * @throws DatabaseNotFoundException 
	 * @throws DriverNotFoundException 
	 */
	private void initialize() throws DatabaseNotFoundException, DriverNotFoundException {
		frmGestoreContratti = new JFrame();
		frmGestoreContratti.setResizable(false);
		frmGestoreContratti.setTitle("Gestore Contratti");
		//Toolkit tk = Toolkit.getDefaultToolkit();		
		//frmGestoreContratti.setSize(tk.getScreenSize());
		frmGestoreContratti.setSize(800, 600);
		frmGestoreContratti.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		String dbPath = "database"+File.separator+"contratti_db.sqlite";					
		databaseController = new DatabaseController(dbPath);
		databaseController.verifyDbExist();
		databaseController.loadDriverClass();
		
		JMenuBar menuBar = new JMenuBar();
		frmGestoreContratti.setJMenuBar(menuBar);
		
		JMenu menuContratti = new JMenu("Contratti");
		menuBar.add(menuContratti);
		
		JMenuItem nuovoContrattoMenuItem = new JMenuItem("Nuovo");
		nuovoContrattoMenuItem.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
				NuovoContrattoFrame nuovoContrattoFrame = new NuovoContrattoFrame(contrattiScadenzaPanel);				
				nuovoContrattoFrame.setVisible(true);						
			}
		});
		menuContratti.add(nuovoContrattoMenuItem);
		
		JMenu mnSpecifiche = new JMenu("Specifiche");
		menuBar.add(mnSpecifiche);
		
		JMenuItem visualizzaSpecificheMenuItem = new JMenuItem("Visualizza Specifiche");
		visualizzaSpecificheMenuItem.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				try {
					Vector<Specifica> specifiche = databaseController.selectSpecifiche();
					VisualizzaSpecificheFrame visualizzaSpecificheFrame = new VisualizzaSpecificheFrame(databaseController, specifiche);
					visualizzaSpecificheFrame.setVisible(true);
				} catch (DatabaseConnectionException ex) {
					JOptionPane.showMessageDialog(null, ex.getMessage(), "", JOptionPane.WARNING_MESSAGE);
				} catch (QueryException ex) {
					JOptionPane.showMessageDialog(null, ex.getMessage(), "", JOptionPane.WARNING_MESSAGE);
				}
			}
		});
		mnSpecifiche.add(visualizzaSpecificheMenuItem);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		frmGestoreContratti.getContentPane().setLayout(gridBagLayout);
		
		contrattiScadenzaPanel = new ContrattiScadenzaPanel(databaseController);
		GridBagConstraints gbc_contrattiScadenzaPanel = new GridBagConstraints();
		gbc_contrattiScadenzaPanel.fill = GridBagConstraints.BOTH;
		gbc_contrattiScadenzaPanel.gridx = 0;
		gbc_contrattiScadenzaPanel.gridy = 0;
		frmGestoreContratti.getContentPane().add(contrattiScadenzaPanel, gbc_contrattiScadenzaPanel);
	}

}
